#include "acceuil.h"
#include "ui_acceuil.h"
#include <vector>
#include <dirent.h>
#include <iostream>
#include <string>
#include <list>
#include <file_util.h>
using namespace std;

Acceuil::Acceuil(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Acceuil)
{
    ui->setupUi(this);
    createActions();
    //createToolbars();
    createRecentFileList();
}

Acceuil::~Acceuil()
{
    delete ui;
}

// ***************************** SLOT *******************************
// File
void Acceuil::newNote()
{
    //    this->hide();
    dialog_new = new NouvelleNoteDialog();
    dialog_new->show();

}
void Acceuil::open()
{

}
void Acceuil::save()
{

}
void Acceuil::saveAs()
{

}
void Acceuil::exportAsPdf()
{

}
void Acceuil::print()
{

}

// Edit
void Acceuil::copy()
{

}
void Acceuil::paste()
{

}
void Acceuil::cut()
{

}
void Acceuil::undo()
{

}

//Help
void Acceuil::help()
{

}

void Acceuil::about(){

}
// other
void Acceuil::openFromHistory(const QString & filename)
{
    mainWindow = new MainWindow( filename);
    mainWindow->show();
    //this->hide();

}
// **************************** Function *****************************
void Acceuil::createActions()
{   
    // *************************** file action ****************************
    ui->actionNew = new QAction(QIcon(":/img/new.png"), tr("&New"), this);
    ui->actionNew->setShortcuts(QKeySequence::New);
    ui->actionNew->setStatusTip(tr("Créer un nouveau fichier"));
    connect(ui->actionNew, SIGNAL(triggered()), this, SLOT(newNote()));

    ui->actionOpen = new QAction(QIcon(":/img/open.png"), tr("&Open..."), this);
    ui->actionOpen->setShortcuts(QKeySequence::Open);
    ui->actionOpen->setStatusTip(tr("Open an existing file"));
    connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(open()));

    ui->actionSave = new QAction(QIcon(":/img/save.png"), tr("&Save"), this);
    ui->actionSave->setShortcuts(QKeySequence::Save);
    ui->actionSave->setStatusTip(tr("Save the document to disk"));
    connect(ui->actionSave, SIGNAL(triggered()), this, SLOT(save()));

    ui->actionSaveAs = new QAction(tr("Save &As..."), this);
    ui->actionSaveAs->setShortcuts(QKeySequence::SaveAs);
    ui->actionSaveAs->setStatusTip(tr("Save the document under a new name"));
    connect(ui->actionSaveAs, SIGNAL(triggered()), this, SLOT(saveAs()));

    ui->actionExit = new QAction(tr("E&xit"), this);
    ui->actionExit->setShortcuts(QKeySequence::Quit);
    ui->actionExit->setStatusTip(tr("Exit the application"));
    connect(ui->actionExit, SIGNAL(triggered()), this, SLOT(close()));

    // *************************** edit action ****************************
    ui->actionCut = new QAction(QIcon(":/img/cut.png"), tr("Cu&t"), this);
    ui->actionCut->setShortcuts(QKeySequence::Cut);
    ui->actionCut->setStatusTip(tr("Cut the current selection's contents to the "
                                   "clipboard"));
    connect(ui->actionCut, SIGNAL(triggered()), this, SLOT(cut()));

    ui->actionCopy = new QAction(QIcon(":/img/copy.png"), tr("&Copy"), this);
    ui->actionCopy->setShortcuts(QKeySequence::Copy);
    ui->actionCopy->setStatusTip(tr("Copy the current selection's contents to the "
                                    "clipboard"));
    connect(ui->actionCopy, SIGNAL(triggered()), this, SLOT(copy()));

    ui->actionPaste = new QAction(QIcon(":/img/paste.png"), tr("&Paste"), this);
    ui->actionPaste->setShortcuts(QKeySequence::Paste);
    ui->actionPaste->setStatusTip(tr("Paste the clipboard's contents into the current "
                                     "selection"));
    connect(ui->actionPaste, SIGNAL(triggered()), this, SLOT(paste()));

    // *************************** help action ****************************
    ui->actionAbout = new QAction(tr("&About"), this);
    ui->actionAbout->setStatusTip(tr("Show the application's About box"));
    connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()));


//    ui->actionCut->setEnabled(false);
//    ui->actionCopy->setEnabled(false);

}
void Acceuil::createToolbars()
{
    ui->mainToolBar->addAction(ui->actionNew);
    ui->mainToolBar->addAction(ui->actionOpen);
    ui->mainToolBar->addAction(ui->actionSave);

    ui->mainToolBar->addSeparator();

    ui->mainToolBar->addAction(ui->actionCopy);
    ui->mainToolBar->addAction(ui->actionCut);
    ui->mainToolBar->addAction(ui->actionPaste);
}

void Acceuil::createRecentFileList()
{
    unsigned int i;
    vector<string> list_vector = file_util::listDirectory(".notes");
    unsigned int max = (list_vector.size() < 10) ? list_vector.size() : 10;

    if(list_vector.size() == 0) ui->label->hide();
    for (i = 0;i < max;i++) {
        vector<string> temp = file_util::decompose_file_name(list_vector[i].c_str(),'_');
        if(temp.size() == 0 || temp[0] == "") continue;
        vector<string> name_titre = file_util::decompose_file_name(temp[0],'#');
        if(name_titre.size() != 2 || name_titre[0] == "") continue;

        string concat_file;
        string filename_to_add = name_titre[0] + ", " + name_titre[1];
        concat_file = "<a href='"+list_vector[i]+"'> " + filename_to_add + "</a>";
        recentFileList[i] = new QLabel(tr(concat_file.c_str()));
        ui->verticalLayout->addWidget(recentFileList[i]);
        connect(recentFileList[i],SIGNAL(linkActivated(QString)),
                this,SLOT(openFromHistory(QString)));
    }
}

void Acceuil::setCurrentFile(const QString &fileName)
{
    curFile = fileName;
    setWindowFilePath(curFile);

    QSettings settings;
    QStringList files = settings.value("recentFileList").toStringList();
    files.removeAll(fileName);
    files.prepend(fileName);
    while (files.size() > MaxRecentFiles)
        files.removeLast();

    settings.setValue("recentFileList", files);

    foreach (QWidget *widget, QApplication::topLevelWidgets()) {
        Acceuil *acceuil = qobject_cast<Acceuil *>(widget);
        if (acceuil)
            acceuil->updateRecentFileList();
    }
}
void Acceuil::updateRecentFileList()
{
    QSettings settings;
    QStringList files = settings.value("recentFileList").toStringList();

    int numRecentFiles = qMin(files.size(), (int)MaxRecentFiles);

    for (int i = 0; i < numRecentFiles; ++i) {
        QString text = tr("&%1 %2").arg(i + 1).arg(strippedName(files[i]));
        text = "<a href>" + text + "</a>";
        recentFileList[i]->setText(text);
        recentFileList[i]->setVisible(true);
    }
    for (int j = numRecentFiles; j < MaxRecentFiles; ++j)
        recentFileList[j]->setText("");

}
QString Acceuil::strippedName(const QString &fullFileName)
{
    return QFileInfo(fullFileName).fileName();
}
void Acceuil::createWelcomeGrid()
{

}
